package com.example.controller.user;

import com.example.domain.user.dto.UserDTO;
import com.example.domain.user.dto.UserUpdateDTO;
import com.example.domain.user.vo.UserVO;
import com.example.service.user.IUserService;
import lombok.extern.slf4j.Slf4j;
import oj.common.core.contants.HttpConstants;
import oj.common.core.controller.BaseController;
import oj.common.core.domain.LoginUserVO;
import oj.common.core.domain.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/user")
@Slf4j
public class UserController extends BaseController {
    @Autowired
    private IUserService userService;
    @PostMapping("/sendCode")
    public Result<Void> sendCode(@RequestBody UserDTO userDTO) {
        return toResult(userService.sendCode(userDTO)) ;
    }
    @PostMapping("/code/login")
    public Result<String> login(@RequestBody UserDTO userDTO) {
        return Result.success(userService.login(userDTO.getPhone(),userDTO.getCode()));
    }
    //退出登录
    @DeleteMapping("/logout")
    public Result<Void> logout(@RequestHeader(HttpConstants.AUTHENTICATION)String token) {
        return toResult(userService.logout(token));
    }
    @GetMapping("/info")
    public Result<LoginUserVO> info(@RequestHeader(HttpConstants.AUTHENTICATION) String token) {
        return userService.info(token);
    }

    @PutMapping("/edit")
    public Result<Void> edit(@RequestBody UserUpdateDTO userUpdateDTO) {
        //当是编辑删除操作的时候，返回的是一个大于或者是一个布尔类型的值
        return toResult(userService.edit(userUpdateDTO));
    }

    @GetMapping("/detail")
    public Result<UserVO> detail() {
        return Result.success(userService.detail());
    }
    @PutMapping("/head-image/update")
    public Result<Void> updateImage(@RequestBody UserUpdateDTO userUpdateDTO) {
        return toResult(userService.updateImage(userUpdateDTO));
    }
}
